﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Guline.Web.XjsGame.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult IndexPaging(int newsPage)
        {
            if (newsPage < 1)
            {
                newsPage = 1;
            }

            var news = db.contents.Where(m => m.status == 0&&m.danhmuc!="0").OrderByDescending(m => m.id);
            int pagesize = 10;
            double total = news.Count();
            int pages = (int)Math.Ceiling(total / pagesize);
            if (newsPage > pages)
            {
                newsPage = pages;
            }

            ViewBag.newsPage = newsPage + 1;
            return View("Index",news.Skip(((int)newsPage - 1) * pagesize).Take(pagesize));
        }
        public ActionResult Index(int? newsPage)
        {
            if (!newsPage.HasValue || newsPage < 1)
            {
                newsPage = 1;
            }

            var news = db.contents.Where(m => m.status == 0 && m.danhmuc != "0").OrderByDescending(m => m.id);
            int pagesize =10;
            double total = news.Count();
            int pages = (int)Math.Ceiling(total / pagesize);
            if (newsPage > pages)
            {
                newsPage = pages;
            }
            
            ViewBag.newsPage = newsPage+1;
            return View(news.Skip(((int)newsPage - 1) * pagesize).Take(pagesize));
        }
        public ActionResult getNewsIndexPage(int newsPage)
        {
            if (newsPage < 1)
            {
                newsPage = 1;
            }
            var news = db.contents.Where(m => m.status == 0 && m.danhmuc != "0").OrderByDescending(m => m.id);
            int pagesize = 10;
            double total = news.Count();
            int pages = (int)Math.Ceiling(total / pagesize);
            if (newsPage > pages)
            {
                newsPage = pages;
                var mynews = news.Skip((newsPage - 1) * pagesize).Take(pagesize);
                var myencode = from n in mynews select new { n.id, n.name, n.date_added, n.image, n.detail_short };
                return Json(new {ismax=true,max=pages}, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var mynews = news.Skip((newsPage - 1) * pagesize).Take(pagesize);
                var myencode = from n in mynews select new { n.id, n.name, n.date_added,n.image, n.detail_short };
                return Json(new { ismax = false, list = myencode }, JsonRequestBehavior.AllowGet);
            }
        }
        public ActionResult About()
        {
            return View();
        }
    }
}
